import 'package:flutter/material.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MyState(),
      appBar: AppBar(
        title: Text("Life Cycle 生命周期"),
      ),
    );
  }
}

// StatefulWidget 有状态组件

class MyState extends StatefulWidget {
  const MyState({Key? key}) : super(key: key);
  @override
  State<MyState> createState() => _MyStateState();
}

class _MyStateState extends State<MyState> {
  int num = 0;

  @override
  void initState() {
    super.initState();
    print('init State');
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print('didChangeDependencies');
  }

  @override
  void didUpdateWidget(covariant MyState oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    print('didUpdateWidget');
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();

    print('deactivate');
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print('销毁 dispose');
  }

  void _increment() {
    setState(() {
      num++;
    });
  }

  void _decrement() {
    setState(() {
      num--;
    });
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    return Center(
      child: Column(
        children: [
          ElevatedButton(onPressed: _decrement, child: Text("递减")),
          Padding(
            padding: EdgeInsets.all(10),
            child: Text("数字 $num"),
          ),
          ElevatedButton(onPressed: _increment, child: Text("递增"))
        ],
      ),
    );
  }
}
